package test.my.com.myapplicationtest.zhe_xian_tu_view.DynamicPathView.controller;

import android.support.v7.view.menu.ListMenuItemView;
import android.view.View;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import test.my.com.myapplicationtest.zhe_xian_tu_view.DynamicPathView.controller.model.DrawModel;
import test.my.com.myapplicationtest.zhe_xian_tu_view.DynamicPathView.controller.utils.TextAttributeUtil;
import test.my.com.myapplicationtest.zhe_xian_tu_view.DynamicPathView.draw.model.CircleModel;
import test.my.com.myapplicationtest.zhe_xian_tu_view.DynamicPathView.draw.model.LineModel;
import test.my.com.myapplicationtest.zhe_xian_tu_view.DynamicPathView.draw.model.PathModel;
import test.my.com.myapplicationtest.zhe_xian_tu_view.DynamicPathView.draw.model.PathXY;
import test.my.com.myapplicationtest.zhe_xian_tu_view.DynamicPathView.draw.model.RectModel;
import test.my.com.myapplicationtest.zhe_xian_tu_view.DynamicPathView.draw.model.TextModel;

/**
 * Author : Cooper
 * Time : 2016/10/9  16:20
 * Description : 收益动态折线计算类
 */

public class CalculateValue {

    // 横向表格线数
    public static final int HORIZONTAL_LINE_NUM = 5;
    // 竖向表格线数
    public static final int VERTICAL_LINE_NUM = 7;
    // 竖向表格宽度系数
    public static final int HORIZONTAL_TABLE_UNIT = 30;
    // 竖向表格高度系数
    public static final int VERTICAL_TABLE_UNIT = 20;
    // 表格线宽
    public static final int TABLE_LINE_STROKE_WIDTH = 2;
    // 折线线宽
    public static final int ZHE_XIAN_LINE_STROKE_WIDTH = 8;

    /**
     * 计算纵坐标起始点X
     * @param maxValueWidth  纵坐标上最大值宽度
     * @return
     */
    public static float getStartVerticalX(View view, float maxValueWidth){
        float x = Constant.LEFT_MARGIN_SCALE*view.getWidth()+Constant.V_TEXT_LINE_MARGIN_SCALE*view.getWidth()+maxValueWidth;
        return x;
    }
    //获取纵坐标起始点Y
    public static float getStartVerticalY(View view){
        float y = Constant.TOP_MARGIN_SCALE*view.getHeight();
        return y;
    }

    //获取纵坐标终点x
    public static float getEndVerticalX(View view, float maxValueWidth){
        return getStartVerticalX(view,maxValueWidth);
    }

    /**
     * 计算纵坐标结束点Y
     * @param view
     * @return
     */
    public static float getEndVerticalY(View view){
        float endY = view.getHeight()-Constant.BOTTOM_MARGIN_SCALE*view.getHeight()-
                Constant.H_TEXT_LINE_MARGIN_SCALE*view.getHeight()- TextAttributeUtil.getTextHeight(Constant.TEXT_SIZE * view.getWidth());
        return endY;
    }

    //获取横坐标终点X

    public static float getEndHorizontalX(View view){
        float horizontalX = view.getWidth()-Constant.RIGHT_MARGIN_SCALE*view.getWidth();
        return horizontalX;
    }

    /**
     * 横坐标结束点的Y值跟纵坐标结束点Y是相等的
     * @return
     */
    public static  float getEndHorizontalY(View view){
        return getEndVerticalY(view);
    }

    /**
     * 获取表格横线models
     * @param view
     * @param maxValueWidth
     * @return
     */
    public static List<LineModel> getTableHorizontalLineModels(View view, float maxValueWidth){

        List<LineModel> list = new ArrayList<>();
        float verticalHeight = getEndVerticalY(view)-Constant.TOP_MARGIN_SCALE*view.getHeight();//纵坐标总高度
        float unitHeight = (verticalHeight-VERTICAL_TABLE_UNIT)/HORIZONTAL_LINE_NUM;//每个刻度所占的单位高度
        float verticalX = getStartVerticalX(view,maxValueWidth);
        float verticalY =Constant.TOP_MARGIN_SCALE*view.getHeight()+VERTICAL_TABLE_UNIT;
        for(int i=0;i<HORIZONTAL_LINE_NUM;i++){
            float startX = verticalX;
            float startY = verticalY+i*unitHeight;
            float endX = getEndHorizontalX(view);
            float endY= startY;

            LineModel dottedLineModel = new LineModel(startX,startY,endX,endY,Constant.LINE_COLOR,TABLE_LINE_STROKE_WIDTH);
            list.add(dottedLineModel);

        }

        return list;

    }

    /**
     * 获取表格竖线models
     * @param view
     * @param maxValueWidth
     * @return
     */
    public static List<LineModel> getTableVerticalModels(View view, float maxValueWidth){
        List<LineModel> list = new ArrayList<>();
        float horizontalWidth = getEndHorizontalX(view)-Constant.RIGHT_MARGIN_SCALE*view.getWidth();//横坐标总宽度
        float unitWidth = (horizontalWidth+HORIZONTAL_TABLE_UNIT)/VERTICAL_LINE_NUM;//每个刻度所占的单位宽度
        float verticalX = getStartVerticalX(view,maxValueWidth);
        float verticalY =CalculateValue.getStartVerticalY(view);
        for(int i=1;i<=VERTICAL_LINE_NUM;i++){
            float startX = verticalX+i*unitWidth;
            float startY = verticalY;
            float endX = verticalX+i*unitWidth;
            float endY= getEndHorizontalY(view);

            LineModel dottedLineModel = new LineModel(startX,startY,endX,endY,Constant.LINE_COLOR,TABLE_LINE_STROKE_WIDTH);
            list.add(dottedLineModel);

        }
        return list;
    }

    /**
     * 获取填充表格的矩形models
     * @param view
     * @param maxValueWidth
     * @return
     */
    public static List<RectModel> getTableFillRectModels(View view, float maxValueWidth) {
        List<RectModel> rectModelList = new ArrayList<>();
        List<LineModel> lineModelList = getTableVerticalModels(view,maxValueWidth);
        float temp = 2;
        RectModel rectModel1 = new RectModel(lineModelList.get(0).getBeginX()+temp,lineModelList.get(0).getBeginY(),
                lineModelList.get(1).getEndX()-temp,lineModelList.get(1).getEndY()-temp,Constant.RECT_COLOR);
        RectModel rectModel2 = new RectModel(lineModelList.get(2).getBeginX()+temp,lineModelList.get(2).getBeginY(),
                lineModelList.get(3).getEndX()-temp,lineModelList.get(3).getEndY()-temp,Constant.RECT_COLOR);
        RectModel rectModel3 = new RectModel(lineModelList.get(4).getBeginX()+temp,lineModelList.get(4).getBeginY(),
                lineModelList.get(5).getEndX()-temp,lineModelList.get(5).getEndY()-temp,Constant.RECT_COLOR);

        rectModelList.add(rectModel1);
        rectModelList.add(rectModel2);
        rectModelList.add(rectModel3);
        return rectModelList;
    }

    /**
     * 计算横坐标时间位置models
     * @param list
     * @param view
     * @param maxValueWidth
     * @return
     */
    public static List<TextModel> getTimeTextModels(List<DrawModel> list, View view, float maxValueWidth) {
        List<LineModel> lineModelList = getTableVerticalModels(view,maxValueWidth);
        List<TextModel> textModelList = new ArrayList<>();
        int size = list.size();
        float textY = getEndVerticalY(view)+Constant.H_TEXT_LINE_MARGIN_SCALE*view.getHeight()+TextAttributeUtil.getTextHeight(Constant.TEXT_SIZE*view.getWidth());
        List<TextModel> textModels = new ArrayList<>();
        for (int i=0;i<size;i++){
            String textValue= list.get(i).getTimeX();
            float textWidth = TextAttributeUtil.getTextWidth(textValue, Constant.TEXT_SIZE*view.getWidth());
            float startX = 0;
            if (i == 0) {
                float tableLeftX = getEndVerticalX(view, maxValueWidth);// 获取纵坐标X
                startX = tableLeftX -(textWidth) / 2;
            } else {
                float lineX = lineModelList.get(i-1).getEndX();// 纵线X
                startX = lineX - (textWidth) / 2;
            }
            TextModel textModel = new TextModel(textValue,Constant.TEXT_SIZE*view.getWidth(),startX,textY,Constant.H_TEXT_COLOR);
            textModels.add(textModel);
        }
        return textModels;
    }

    /**
     * 业务点在图中的坐标点
     * @param list
     * @param view
     * @param minValue
     * @param maxValue
     * @return
     */
    public static List<PathXY> getPointModelList(List<DrawModel> list, View view, float minValue, float maxValue,float maxValueWidth){
        List<PathXY> pointModels = new ArrayList<>();
        float drawH = getEndVerticalY(view)-Constant.TOP_MARGIN_SCALE*view.getHeight();//计算绘制Y轴高度
        float perH = drawH/(maxValue-minValue);
        int size = list.size();
        float horizontalWidth = getEndHorizontalX(view)-Constant.RIGHT_MARGIN_SCALE*view.getWidth();//横坐标总宽度
        float unitWidth = (horizontalWidth+HORIZONTAL_TABLE_UNIT)/size;//每个刻度所占的单位宽度
        for(int i =0;i<size;i++){
            float f = list.get(i).getPriceY();
            float valueH = drawH-perH*(f-minValue);
            float y = getStartVerticalY(view)+valueH;
            float x = getStartVerticalX(view,maxValueWidth)+i*unitWidth;
            PathXY pathXY = new PathXY(x,y);
            pointModels.add(pathXY);
        }

        return pointModels;
    }

    /**
     * 两个业务点之间的点坐标
     * @param list
     * @param view
     * @param minValue
     * @param maxValue
     * @return
     */
    public static List<PathXY> getPathList(List<DrawModel> list, View view, float minValue, float maxValue, float maxValueWidth){

        List<PathXY> originPathList = getPointModelList(list,view,minValue,maxValue,maxValueWidth);
        List<PathXY> handPointList = new ArrayList<>();
        int size = originPathList.size();
        if(size>0){
            for(int i=0;i<size;i++){
                if(i>0){
                    PathXY pathXY1 = originPathList.get(i-1);
                    PathXY pathXY2 = originPathList.get(i);
                    float x1 = pathXY1.getX();
                    float y1 = pathXY1.getY();
                    float x2 = pathXY2.getX();
                    float y2 = pathXY2.getY();
                    List<PathXY> paths = getPathAllPoints(x1,y1,x2,y2);
                    handPointList.addAll(paths);
                }
            }

            handPointList.add(0,originPathList.get(0));
        }
        return handPointList;
    }

    public static List<PathXY> getPathAllPoints(float x1, float y1, float x2, float y2) {
        List<PathXY> list = new ArrayList<>();
        float a;
        float b;
        if (x1 == x2) {//横坐标相等
            float tempY = y1;
            while(true){
                PathXY point = new PathXY();
                point.setX(x1);
                if (y1 > y2) {
                    if (tempY > y2) {
                        tempY=tempY-1;
                        if(tempY<=y2){
                            tempY=y2;
                        }
                        point.setY(tempY);
                        list.add(point);
                    } else {
                        break;
                    }
                } else {
                    if (tempY < y2) {
                        tempY=tempY+1;
                        if(tempY>=y2){
                            tempY=y2;
                        }
                        point.setY(tempY);
                        list.add(point);
                    }else {
                        break;
                    }
                }

            }
        } else if (y1 == y2) {//纵坐标相等
            float tempX = x1;
            while (true){
                PathXY point = new PathXY();
                if (tempX < x2) {
                    tempX = tempX + 1;
                    if(tempX>=x2){
                        tempX=x2;
                    }
                    point.setX(tempX);
                    point.setY(y1);
                    list.add(point);

                } else {
                    break;
                }
            }
        } else {
            a = (y2 - y1) / (x2 - x1);
            b = y2 - a * x2;
            float tempX = x1;
            float tempY = 0;
            while (true){
                PathXY point = new PathXY();
                if (tempX <x2) {
                    tempX = tempX + 0.5f;
                    if(tempX>=x2){
                        tempX=x2;
                        tempY=y2;
                    }
                    point.setX(tempX);
                    tempY = a * tempX + b;
                    if(tempX==x2){
                        tempY=y2;
                    }
                    point.setY(tempY);
                    list.add(point);
                } else {
                    break;
                }
            }
        }

        return list;
    }

    //根据pathxy 获取pathModel
    public static PathModel getPathModels(List<PathXY> pathList, View view, boolean isFill){
        PathModel pathModel;
        List<PathXY> pathXYList = new ArrayList<>(pathList);
        if(isFill) {
            if (pathList.size() > 0) {
                PathXY pathXY = new PathXY();
                PathXY pathXY1 = pathList.get(0);
                pathXY.setX(pathXY1.getX());
                pathXY.setY(getEndVerticalY(view)-2);
                pathList.add(0, pathXY);

                PathXY lastPath = new PathXY();
                PathXY pathXY2 = pathList.get(pathList.size() - 1);
                lastPath.setX(pathXY2.getX());
                lastPath.setY(getEndVerticalY(view)-2);
                pathList.add(lastPath);
            }
            pathModel = new PathModel(pathList, isFill, Constant.FILL_COLOR, ZHE_XIAN_LINE_STROKE_WIDTH);
        }else {
            pathModel = new PathModel(pathXYList, isFill, Constant.PATH_COLOR, ZHE_XIAN_LINE_STROKE_WIDTH);
        }
        return pathModel;
    }

    /**
     * 获取两个集合内相同的点
     * @param list1
     * @param list2
     * @return
     */
    public static List<PathXY> getDotsList(List<PathXY> list1, List<PathXY> list2){
        HashMap<String,PathXY> hashMap = new HashMap<>();
        List<PathXY> list = new ArrayList<>();
        for(PathXY pathXY:list2){
            float x = pathXY.getX();
            float y = pathXY.getY();
            hashMap.put(x+"_"+y,pathXY);
        }

        for(PathXY pathXY:list1){
            String key = pathXY.getX()+"_"+pathXY.getY();
            if(hashMap.containsKey(key)){
                list.add(pathXY);
            }
        }
        return list;
    }
}
